package com.start.demo.user.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.start.demo.user.model.User;
import com.start.demo.user.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	UserService userService;

	/**
	 * 展示用户列表
	 * 
	 * @param user
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String listUser(Model model) {
		User user = new User();
		List<User> list = userService.queryUserList(user);
		model.addAttribute("list", list);
		return "userList";
	}

	/**
	 * 根据id查询
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/get/{id}", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getUser(@PathVariable String id) {
		User user = userService.getUserById(id);
		if (null == user) {
			return "[]";
		}
		return user.toString();
	}

}
